package cn.edu.scau.cmi.yangjiamin.client;

import java.util.List;
import cn.edu.scau.cmi.yangjiamin.singleormulti.ChairmanMulti;
import cn.edu.scau.cmi.yangjiamin.singleormulti.ChairmanSingle;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.control.Button;

public class SingleOrMultiClient extends Application{
	@Override
	public void start(Stage primaryStage) throws Exception{
		BorderPane pane = new BorderPane();
		HBox hbox = new HBox();
		Button single = new Button("单例模式创建主席");
		ChairmanSingle cs1 = ChairmanSingle.getInstance("毛泽东");
		Button multi = new Button("多例模式创建主席");
		hbox.getChildren().addAll(single, multi);
		Text text =  new Text(100, 100, "显示查询结果");
		single.setOnAction(e->{
			ChairmanSingle cs2 = ChairmanSingle.getInstance("other");
			text.setText(cs2.getName());
		});
		
		List<ChairmanMulti> list = ChairmanMulti.addInstance("毛泽东");
		ChairmanMulti.addInstance("刘少奇");
		ChairmanMulti.addInstance("江泽民");
		ChairmanMulti.addInstance("胡锦涛");
		
		
		multi.setOnAction(e->{
			ChairmanMulti.addInstance("胡锦涛");
			StringBuilder sb = new StringBuilder();
			sb.append("最多只能添加" + list.size() + "个主席");
			for(ChairmanMulti cm : list) {
				sb.append(cm.getName() + " ");
			}	
			text.setText(sb.toString());
		});
		pane.setTop(hbox);
		pane.setCenter(text);
		Scene scene = new Scene(pane, 600, 500);
		primaryStage.setTitle("singleOrMultiClient");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
}
